<?php
class Mklaim extends CI_Model {

		function Mklaim(){
				parent::__construct();
        }
		
        function getTerritory($pid = 1, $type="")
        {    
                $dblokal  = $this->load->database("default", TRUE);
                $dblokal->order_by("territory_name", "ASC");
                $dblokal->where("parent_id", $pid);
				if($type <> "") {
					$dblokal->where(array("territory_type_id"=> $type));
				}
                $query = $dblokal->get("t_mtr_territory");
                return $query;
		}
		
		function getKotaOutlet($pid)
        {    
                $dblokal  = $this->load->database("default", TRUE);
                
				$query = $dblokal->query("SELECT DISTINCT(city) FROM t_mtr_territory,t_mtr_outlet
										 WHERE t_mtr_territory.territory_id=t_mtr_outlet.territory_id AND
										 parent_id = '".$pid."'
										 ORDER BY city");
                return $query;
		}
		
		function getSales($id="",$val="")
        {    
                $dblokal  = $this->load->database("default", TRUE);
				$sql = "";
				if($id <> ""){
						$sql = ($val==""?"AND UPPER(user_id) like '%".strtoupper($id)."%'":"AND UPPER(user_id) like '".strtoupper($id)."'");
				}
				$query = $dblokal->query("SELECT *
										 FROM t_mtr_user
										 WHERE user_group_id IN (8,9) ".$sql."
										 ORDER BY user_id LIMIT 20");
                return $query->result();
				
		}
		
		function getOutlet($id="",$val="")
        {
				$dblokal  = $this->load->database("default", TRUE);
				
				$sql = "UPPER(outlet_id) like '%".strtoupper($id)."%'";
				if($val <> ""){
						$sql = "UPPER(outlet_id) = '".strtoupper($id)."'";
				}
                
                $query = $dblokal->query("SELECT *
										 FROM t_mtr_territory,t_mtr_outlet
										 WHERE t_mtr_territory.territory_id=t_mtr_outlet.territory_id AND
										 ".$sql."
										 ORDER BY territory_name
										 LIMIT 20");
                return $query->result();
		}
		
		function getProduk($id)
        {    
                $dblokal  = $this->load->database("default", TRUE);
                $query = $dblokal->query("SELECT item_id as id, item_id as label, item_id as value, item_name as name
										 FROM t_mtr_item
										 WHERE UPPER(item_id) like '%".strtoupper($pid)."%'
										 ORDER BY item_name");
                return $query->result();
		}
		
		function getProgram(){
			$dblokal  = $this->load->database("default", TRUE);
			$query = $dblokal->query("select * from v_combo_card_claim_program");
			return $query;
		}
		
		function getFrontliner($outlet_id){
			$dblokal  = $this->load->database("default", TRUE);
			$query = $dblokal->query("SELECT outlet_frontliner_id , frontliner_name, frontliner_identity_no
									 FROM t_mtr_outlet_frontliner
										WHERE outlet_id = '".$outlet_id."' "); //
			return $query;
		}
		
		function saveFrontliner(){
				$dblokal  = $this->load->database("default", TRUE);
				$data['frontliner_name'] = $this->input->post('nama');
				$data['outlet_id'] = $this->input->post('outlet_id');
				
				$dblokal->insert('t_mtr_outlet_frontliner',$data);
		}
		
		function getProduct(){
				$dblokal  = $this->load->database("default", TRUE);
				$query = $dblokal->query("SELECT DISTINCT item_name
										 FROM t_mtr_item WHERE istatus = 1 ");
				return $query;
		}
		
		function getMDN($id){
				$dblokal  = $this->load->database("default", TRUE);
				$query = $dblokal->query("SELECT iccid 
										 FROM t_mtr_item WHERE iccid like '%".$id."%' LIMIT 20");
				return $query->result();
		}
		
		function getProductMDN($id){
				$dblokal  = $this->load->database("default", TRUE);
				$query = $dblokal->query("SELECT * 
										 FROM t_mtr_item WHERE iccid = '".$id."'");
				return $query->result();
		}
		
		function saveKlaim(){
				
				$region= $this->input->post('region');
				$cluster= $this->input->post('cluster');
				$kota= $this->input->post('kota');
				$ot = explode(" :: ",$this->input->post('outlet'));
				$outlet= $this->mglobal->showdata('territory_id','t_mtr_outlet',array('outlet_id'=>$ot[0]),'dblokal');
				$frontliner= $this->input->post('frontliner');
				$tanggal= $this->input->post('tanggal');
				$program= $this->input->post('program');
				
				$mdn= $this->input->post('mdn');
				
				$data = array('claim_date'=>$tanggal,
							  'user_id'=>$this->session->userdata('username'),
							  'territory_id'=>$outlet,
							  'requestor'=>$frontliner,
							  'program'=>$program,
							  'istatus'=>1,
							  'created_by'=>$this->session->userdata('username'),
							  'created_on'=>date('Y-m-d H:i:s'),
							  'acess_via'=>'web');
				
				$dblokal  = $this->load->database("default", TRUE);
				$dblokal->insert('t_trx_card_claim',$data);
				
				$id = $this->db->insert_id();
				#masukkan detail datanya di sini
				for($i=0;$i<count($mdn);$i++){
						#proses simpan data
						$idata = array('claim_id'=>$id,
										'iccid'=>$mdn[$i]['value'],
										'istatus'=>1);
						$dblokal->insert('t_trx_card_claim_detail',$idata);
				}
		}
		
		function read(){
				$dblokal  = $this->load->database("default", TRUE);
				$query = $dblokal->query("SELECT * FROM
										 t_trx_card_claim, t_mtr_territory, t_mtr_outlet_frontliner
										 WHERE t_trx_card_claim.territory_id = t_mtr_territory.territory_id
												AND t_trx_card_claim.requestor and t_mtr_outlet_frontliner.outlet_frontliner_id
												AND t_trx_card_claim.istatus=1");
				return $query->result();
		}
		function edit(){
				
		}
		function delete(){
				$id=$this->input->post('id');
				$dblokal  = $this->load->database("default", TRUE);
				$query = $dblokal->query("UPDATE t_trx_card_claim
										 SET istatus=0
										 WHERE claim_id='".$id."'");
		}
}
?>